from compare_image import *
from imports import *

# requires Image object
def get_chunk(image, height, width):
    image_x, image_y = image.size
    for i in range(image_y / height):
        for j in range(image_x / width):
            box = (j * width, i * height, (j+1) * width, (i+1)*height)
            return image.crop(box).convert()
        
def parse_level(image, height, width, out_file):
    out_file = open(out_file, 'w')
    image_x, image_y = image.size
    done = 0.0
    total = image_x / width * image_y / height
    for i in range(image_y / height):
        for j in range(image_x / width):
            box = (j * width, i * height, (j+1) * width, (i+1)*height)
            chunk = image.crop(box).convert()
            # brick.bmp x y
            block_type = get_block_type(chunk, block_images)
            if block_type != None:
                out_file.write("%s %d %d\n" % (block_type, j * 32, i * 32))
#                print block_type
            done += 1
            if done % 50 == 0:
                print "parsed %d%%" % (done/total * 100)
                #print "parsed: %d of %d" % (done, total)
    print "parsing of %d chunks complete." % done
